//应收管理
/**
 * fixed by zhaojian
 */
/**
 * 总视图
 */
interface IReceivableManagentViewModel extends IBaseViewModel<IReceivableManagentTQueryTerms>{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<FinanceReceivableListItemResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 列表高度设置
     */
    Lheight:number;
    /**
     * 回单状态下拉
     */
    receiptStatusDropDown:ValueListItemResponse[];
    /**
     * 结算状态下拉
     */
    settleStatusDropDown:ValueListItemResponse[];
    /**
     * 应收费用类型下拉
     */
    receivableFeeTypeDropDown:ValueListItemResponse[];
    /**
     * 订单状态下拉
     */
    orderStatusDropDown:ValueListItemResponse[];
    /**
     * 结算单据状态下拉
     */
    receiptBillStatusDropDown:ValueListItemResponse[];
    /**
     * 查看订单应付详情
     */
    getOrderFeeDetail:(carrierOrderId:string,orderId:string,orderChildId:string,id:string,receivableCode:string) => void;
    /**
     * 导出报表是否启用
     */
    expectDis:boolean;
    /**
     * 费用合计
     */
    totalReceivable:number;
    
}

/**
 * 查询条件
 */
interface IReceivableManagentTQueryTerms{
    /**
     * 客户单位
     */
    clientName:string;
    /**
     * 结算单位
     */
    settle:string;
    /**
     * 应收总编号
     */
    receivableSettleBillCode:string;
    /**
     * 合同号
     */
    contractNumber:string;
    /**
     * 车牌号
     */
    carCode:string;
    /**
     * 发货起始时间
     */
    startTime:string;
    /**
     * 发货结束时间
     */
    endTime:string;
    /**
     * 回单状态
     */
    receiptStatus:string;
    /**
     * 结算状态
     */
    settleStatus:string;
    /**
     * 发货地址
     */
    origin:string;
    /**
     * 送货地址
     */
    destination:string;
    /**
     * 应收费用类型
     */
    feeType:string;
    /**
     * 订单状态
     */
    orderStatus:string;
    /**
     * 货物名称
     */
    goodsName:string;
    /**
     * 结算单据状态
     */
    receiptBillStatus:string
}

class ReceivableManagentController extends ListController<IReceivableManagentViewModel,IReceivableManagentTQueryTerms>{
    gridApi:uiGrid.IGridApiOf<FinanceReceivableListItemResponse>;
    orderBy:string = "0";
    asc:boolean = true;
    skip:number=0;

    constructor($scope:IReceivableManagentViewModel,private authorizationService:IAuthorizationService,private exportService:IexportService,private valueService:IValueService,private receivableService:IReceivableService,
    private $state:angular.ui.IStateService,private $location:ng.ILocationService,private $ngBootbox: BootboxService){
        super($scope);
        this.init();
    }

    init():void{
        super.init();

        this.$scope.getOrderFeeDetail = this.getOrderFeeDetail;
        this.$scope.expectDis = false;
        this.paginationOptions.pageSize = 50,
        this.$scope.queryTerms = {
            clientName:"",
            settle:"",
            receivableSettleBillCode:"",
            contractNumber:"",
            carCode:"",
            startTime:"",
            endTime:"",
            receiptStatus:"",
            settleStatus:"",
            origin:"",
            destination:"",
            feeType:"",
            orderStatus:"",
            goodsName:"",
            receiptBillStatus:''
        }

        this.$scope.gridOptions = {
            data: [],
            paginationPageSizes: [50, 60, 70],
            useExternalPagination: true,
            exporterMenuCsv: false,
            useExternalSorting: false,
            enableColumnResizing: true,
            exporterMenuPdf: false,
            enableGridMenu: true,
            columnDefs: [
                {displayName: "应收总编号", field: 'receivableSettleBillCode', enableColumnMenu: false, width: '8%', cellTooltip:true, headerTooltip: true},
                {displayName: "应收编号", field: 'receivableCode', enableColumnMenu: false, width: '8%', cellTooltip:true, headerTooltip: true},
                {displayName: "应收费用类型", field: 'feeType', enableColumnMenu: false, width: '7%', cellTooltip:true, headerTooltip: true},
                {displayName: "应收说明", field: 'receivableSummary', enableColumnMenu: false, width: '7%', cellTooltip:true, headerTooltip: true},
                {displayName: '客户单位', field: 'client', enableColumnMenu: false, width: '9%', cellTooltip:true, headerTooltip: true},
                {displayName: '结算单位', field: 'settle', enableColumnMenu: false, width: '9%', cellTooltip:true, headerTooltip: true},
                {displayName: '发货地址', field: 'originAddress', enableColumnMenu: false, width: '9%', cellTooltip:true, headerTooltip: true,},
                {displayName: '送货地址', field: 'destinationAddress', enableColumnMenu: false, width: '9%', enableSorting: false, cellTooltip:true, headerTooltip: true},
                {displayName: '发货日期', field: 'realDeliveryTime', enableColumnMenu: false, width: '6%', cellTooltip:true, headerTooltip: true},
                {displayName: '货物名称', field: 'goodsName', enableColumnMenu: false, cellTooltip:true, headerTooltip: true},
                {displayName: '货物数量', field: 'quantityOfGoods', enableColumnMenu: false, enableSorting: false, cellTooltip:true, headerTooltip: true},
                {displayName: '车长', field: 'carLength', enableColumnMenu: false, enableSorting: false, cellTooltip:true, headerTooltip: true},
                {displayName: '应收单价', field: 'receivablePrice', enableColumnMenu: false, cellTooltip:true, headerTooltip: true},
                {displayName: '应收总价', field: 'receivableTotalPrice', enableColumnMenu: false, enableSorting: false, cellTooltip:true, headerTooltip: true},
                {displayName: '合同号', field: 'contractNumber', enableColumnMenu: false, cellTooltip:true, headerTooltip: true},
                {displayName: '车牌号', field: 'carCode', enableColumnMenu: false, cellTooltip:true, headerTooltip: true},
                {displayName: '结算状态', field: 'settleStatus', enableColumnMenu: false, cellTooltip:true, headerTooltip: true},
                {displayName: '回单状态', field: 'receiptStatus', enableColumnMenu: false, cellTooltip:true, headerTooltip: true},
                {
                    displayName: "操作",
                    field: 'orderStatus',
                    cellTemplate: '<a class="fa fa-eye m-l-xs  primary text-info" title="查看详情" ng-click="grid.appScope.getOrderFeeDetail(row.entity.carrierOrderId,row.entity.orderId,row.entity.orderChildId,row.entity.id,row.entity.receivableCode)" ></a>', enableColumnMenu: false, enableSorting: false, width: '4%'}
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope,(skipSize,pageSize)=>{
                    this.paginationChanged(skipSize,pageSize,this.$state);
                });
                // gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
                gridApi.core.on.sortChanged(this.$scope, this.sortChange);
            }
        }

        this.loadStatusDropDown();
        
        $('#openstarttime').click(function () {
            $('#starttime').datetimepicker('show');
        });
        $('#openendtime').click(function () {
            $('#endtime').datetimepicker('show');
        });
        this.query();
        this.authorizationExcel();

    }

    localHistory():void{
        super.localHistory(this.$state)
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }
    
    /**
     * 导出报表
     */
    exportFunc=()=>{
        if(this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            this.exportService.exportReceivable(Number(this.orderBy),this.asc,this.$scope.queryTerms.clientName,this.$scope.queryTerms.settle,"","",this.$scope.queryTerms.startTime,this.$scope.queryTerms.endTime,this.$scope.queryTerms.carCode,
            this.$scope.queryTerms.contractNumber, this.$scope.queryTerms.receiptStatus,this.$scope.queryTerms.settleStatus,this.$scope.queryTerms.origin,this.$scope.queryTerms.destination, "",null, "",this.$scope.queryTerms.feeType,false,
            this.$scope.queryTerms.receivableSettleBillCode,this.$scope.queryTerms.orderStatus,this.$scope.queryTerms.goodsName,"",null,"","");
        } 
    }

    loadData(queryTerms:IReceivableManagentTQueryTerms,skip:number,count:number):void{
        this.receivableService.receivablelist(this.orderBy.toString(),this.asc,queryTerms.clientName,queryTerms.settle,"","",queryTerms.startTime,
        queryTerms.endTime,queryTerms.carCode,queryTerms.contractNumber,queryTerms.receiptStatus,queryTerms.settleStatus,queryTerms.origin,queryTerms.destination,
        "",null,"",queryTerms.feeType,false,queryTerms.receivableSettleBillCode,queryTerms.orderStatus,queryTerms.goodsName,queryTerms.receiptBillStatus,"","",skip,count).then((result)=>{
            // this.$scope.gridOptions.totalItems = result.data.extData;
            this.$scope.gridOptions.data = result.data.data;
            this.$scope.Lheight = ((this.$scope.gridOptions.data.length * 39) + 80);
           
            if (skip == 0) {this.$scope.gridOptions.paginationCurrentPage = 1;}
            this.skip =skip;

            this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
        });
    }
    getListCount():void{
        /**应收合计 */
        this.getTotalReceivable();
        this.receivableService.receivablecount(this.$scope.queryTerms.clientName,this.$scope.queryTerms.settle,"","",this.$scope.queryTerms.startTime,
        this.$scope.queryTerms.endTime,this.$scope.queryTerms.carCode,this.$scope.queryTerms.contractNumber,this.$scope.queryTerms.receiptStatus,this.$scope.queryTerms.settleStatus,this.$scope.queryTerms.origin,this.$scope.queryTerms.destination,
        "",null,"",this.$scope.queryTerms.feeType,false,this.$scope.queryTerms.receivableSettleBillCode,this.$scope.queryTerms.orderStatus,this.$scope.queryTerms.goodsName,this.$scope.queryTerms.receiptBillStatus,"","").then(res=>{
            this.$scope.gridOptions.totalItems = res.data.extData;
        })
    }

    /**
     * 排序
     */
    sortChange = (grid: uiGrid.IGridInstanceOf<FinanceReceivableListItemResponse>, sortColumns: Array<uiGrid.IGridColumnOf<FinanceReceivableListItemResponse>>) => {
        if (sortColumns.length == 0) {
            this.reload();
        }else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "receivableSettleBillCode": {
                    this.orderBy = "1";
                } break;
                case "receivableCode": {
                    this.orderBy = "2";
                } break;
                case "feeType": {
                    this.orderBy = "3";
                } break;
                case "receivableSummary": {
                    this.orderBy = "4";
                } break;
                case "client": {
                    this.orderBy = "5";
                } break;
                case "settle": {
                    this.orderBy = "6";
                } break;
                case "originAddress": {
                    this.orderBy = "7";
                } break;
                case "realDeliveryTime": {
                    this.orderBy = "8";
                } break;
                case "goodsName":{
                    this.orderBy = "9";
                }break;
                case "receivablePrice": {
                    this.orderBy = "10";
                } break;
                case "contractNumber": {
                    this.orderBy = "11";
                } break;
                case "carCode":{
                    this.orderBy = "12";
                }break;
                case "settleStatus":{
                    this.orderBy = "13";
                }break;
                case "receiptStatus":{
                    this.orderBy = "14";
                }break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.reload();
        }
    }

    /**
     * 状态下拉
     */
    loadStatusDropDown():void{
        this.$scope.receiptStatusDropDown = this.valueService.getReceiptStatusList().data;
        this.$scope.settleStatusDropDown = this.valueService.getSettleStatusList().data;
        this.$scope.receivableFeeTypeDropDown = this.valueService.getReceivableFeeTypeList().data;
        this.$scope.orderStatusDropDown = this.valueService.getOrderStatusList().data;
        this.$scope.receiptBillStatusDropDown = this.valueService.getReceiptBillStatusDropDown().data;
    }

    /**
     * 费用合计
     */
    getTotalReceivable = () =>{
        this.receivableService.getTotalReceivable(this.$scope.queryTerms.clientName,this.$scope.queryTerms.settle,"","", this.$scope.queryTerms.startTime,this.$scope.queryTerms.endTime,this.$scope.queryTerms.carCode, 
          this.$scope.queryTerms.contractNumber,this.$scope.queryTerms.receiptStatus, this.$scope.queryTerms.settleStatus, this.$scope.queryTerms.origin,this.$scope.queryTerms.destination,"",null,"", 
          this.$scope.queryTerms.feeType, false, this.$scope.queryTerms.receivableSettleBillCode,this.$scope.queryTerms.orderStatus, this.$scope.queryTerms.goodsName,this.$scope.queryTerms.receiptBillStatus,"","").then((res) =>{
            this.$scope.totalReceivable = res.data.totalReceivable;
        });
    }

    /**
     * 查看订单应付详情
     */
    getOrderFeeDetail = (carrierOrderId:string,orderId:string,orderChildId:string,id:string,receivableCode:string) =>{
        this.$state.go("app.receivable.receivable_reconcile", { carrierOrderId:carrierOrderId, orderId:orderId, orderChildId:orderChildId, Id:id, receivableCode:receivableCode, name: 'receivableManage' });
    }

    /**
     * 判断是否有导出权限
     */
    authorizationExcel = () => {
        this.authorizationService.auth("25B5C4744B62B248E85CFABA3575091F8F678BF92FFFB6A1B6BF499F9C8261F3").then((res) =>{
            this.$scope.expectDis = res.data.extData;
        },(rej)=>{});
    }
}

angular.module("tms").controller("receivableManagentController", ["$scope","authorizationService","exportService","valueService","receivableService","$state","$location","$ngBootbox", ReceivableManagentController]);